/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExamName;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamNameService;
import com.lczyfz.campusinfo.modules.campusinfopay.entity.CampusinfoPay;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExam;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamService;

import java.util.List;

/**
 * 模拟考试管理Controller
 *
 * @author yt
 * @version 2017-10-20
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoexam/campusinfoExam")
public class CampusinfoExamController extends BaseController {
    @Autowired
    private CampusinfoPayService campusinfoPayService;
    @Autowired
    private CampusinfoExamService campusinfoExamService;
    @Autowired
    private CampusinfoExamNameService campusinfoExamNameService;
    @ModelAttribute
    public CampusinfoExam get(@RequestParam(required = false) String id) {
        CampusinfoExam entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoExamService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoExam();
        }
        return entity;
    }

    @RequiresPermissions("campusinfoexam:campusinfoExam:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoExam campusinfoExam, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoExam> page = campusinfoExamService.findPage(new Page<CampusinfoExam>(request, response), campusinfoExam);
        List<CampusinfoExam> campusinfoExams = campusinfoExamService.findList(campusinfoExam);
        model.addAttribute("campusinfoExams", campusinfoExams);
        model.addAttribute("page", page);
        return "modules/campusinfoexam/campusinfoExamList";
    }

    @RequiresPermissions("campusinfoexam:campusinfoExam:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoExam campusinfoExam, Model model) {
        CampusinfoExamName campusinfoExamName = new CampusinfoExamName();
        List<CampusinfoExamName> campusinfoExamNames = campusinfoExamNameService.findList( campusinfoExamName);
        model.addAttribute("campusinfoExamNames", campusinfoExamNames);
        model.addAttribute("campusinfoExam", campusinfoExam);
        return "modules/campusinfoexam/campusinfoExamForm";
    }

    @RequiresPermissions("campusinfoexam:campusinfoExam:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoExam campusinfoExam, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoExam)) {
            return form(campusinfoExam, model);
        }
        campusinfoExam.setUser(UserUtils.getUser());
        campusinfoExamService.save(campusinfoExam);
        addMessage(redirectAttributes, "保存模拟考试信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoexam/campusinfoExam/?repage";
    }

    @RequiresPermissions("campusinfoexam:campusinfoExam:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoExam campusinfoExam, RedirectAttributes redirectAttributes) {
        campusinfoExamService.delete(campusinfoExam);
        addMessage(redirectAttributes, "删除模拟考试信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoexam/campusinfoExam/?repage";
    }
    @RequiresPermissions("campusinfoexam:campusinfoExam:edit")
    @RequestMapping(value = "status")
    public String status(CampusinfoExam campusinfoExam, RedirectAttributes redirectAttributes) {
        campusinfoExam.setStatus(CampusinfoOrderStat.DONE.getValue());
        campusinfoExamService.status(campusinfoExam);
        CampusinfoPay campusinfoPay = new CampusinfoPay();
        campusinfoPay.setId(campusinfoExam.getOrderCode());
        campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
        campusinfoPayService.status(campusinfoPay);
        addMessage(redirectAttributes, "修改模拟考试支付状态");
        return "redirect:" + Global.getAdminPath() + "/campusinfoexam/campusinfoExam/?repage";
    }


}